import { getCurrentPlatform } from "../Common";
import Axios from "axios";
import { cachedGet } from "../Cache";

const DETAILS_URL = '/info/details';
const DETAILS_PUT_URL = '/info/details';
const TAILING_DETAILS_URL = '/tailing/name'
const TAILING_DETAILS_PUT_URL = '/tailing/details'

export async function getTailingInfoDetails(tailingNumber , unitId){
    let config = {
        params:{
            tailingNumber : tailingNumber,
            unitId : unitId
        }
    };
    try{
        let result = await Axios.get(TAILING_DETAILS_URL,config)
        return result.data.parameter
    }catch (e){
        console.error(e);
        throw e;
    }
}

async function loadEnterpriseDetails (useCache, cacheTime) {
    if (useCache === undefined) useCache = true;
    if (cacheTime === undefined) cacheTime = 60 * 1000;        // 默认缓存1*60*1000 ms，即一分钟。
    try {
        const currentPlatform = await getCurrentPlatform.call(this);
        const details = await cachedGet(DETAILS_URL, { params: { id: currentPlatform.unitId }, cache: useCache, cacheTime: cacheTime });
        return details.data.parameter.unitInfoView;
    } catch (e) {
        console.error("加载企业详细信息失败");
        throw e;
    }
}

export async function getEnterpriseDetails (useCache) {
    return await loadEnterpriseDetails.call(this, useCache);
}

export async function setEnterpriseDetails (details) {
    try {
        console.log(details)
        await Axios.put(DETAILS_PUT_URL, details);
        await Axios.put(TAILING_DETAILS_PUT_URL, details)
    } catch (e) {
        console.error("更改企业详细信息失败");
        throw e;
    }
}
